// 使用lazy Suspense 实现懒加载
import React, { lazy, Suspense } from 'react'
import { Navigate, Outlet, useRoutes } from 'react-router-dom'
import Loading from "@/components/Loading";
import AuthComponent from '@/components/AuthComponent';
import Index from "@/pages/Index";
import {
  DatabaseOutlined,
  HomeOutlined,
  MedicineBoxOutlined,
  UserOutlined,
  UnorderedListOutlined,
  BranchesOutlined
} from "@ant-design/icons";

const lazyLoading = (path: string) => {
  const Com = lazy(() => import('@/pages/' + path))
  return (
    <Suspense fallback={<Loading />}>
      <Com />
    </Suspense>
  )
}


export const routes = [
  {
    path: '/',
    element: (
      <AuthComponent>
        <Index />
      </AuthComponent>
    ),
    children: [
      {
        label: "主页",
        icon: <HomeOutlined />,
        path: "/",
        element: lazyLoading("Home")
      },
      {
        label: "数据管理",
        path: "/cmn",
        icon: <DatabaseOutlined />,
        element: <Outlet />,
        // 三级路由
        children: [
          {
            isHide: true,//是否隐藏
            path: "/cmn",
            element: <Navigate to={'dict'} />
          }, {
            label: "数据字典",
            path: '/cmn/dict',
            element: lazyLoading('cmn/dict')
          }
        ]
      },
      {
        label: '医院管理',
        path: '/hospital',
        icon: <MedicineBoxOutlined />,
        element: <Outlet />,
        children: [
          {
            path: "/hospital", // 路径
            isHide: true,//是否隐藏
            element: <Navigate to={"hospitalSet"} />
          }, {
            //医院设置
            label: '医院设置',
            path: '/hospital/hospitalSet',
            element: lazyLoading('hospital/hospitalSet')
          },
          {
            // 医院列表
            label: "医院列表",
            path: "/hospital/hospitalList",
            element: lazyLoading("hospital/hospitalList"),
          },
          {
            // 医院详情
            label: "查看详情",
            isHide: true,
            path: "/hospital/show/:id",
            element: lazyLoading("hospital/show")
          },
          {
            // 医院排班
            label: '医院排班',
            isHide: true,
            path: '/hospital/department/:hoscode', // 传参
            element: lazyLoading('hospital/department')
          }
        ]
      },
      {
        label: '订单管理',
        path: '/order',
        icon: <UserOutlined />,
        element: <Outlet />,
        children: [
          {
            path: "/order",
            isHide: true,
            element: <Navigate to="/order/orderList" />
          },
          {
            path: '/order/orderList',
            label: '订单列表',
            element: lazyLoading('order/orderList')
          },
          {
            isHide: true, // 侧边栏隐藏
            path: '/order/show/:id',
            label: '订单列表',
            element: lazyLoading('order/show')
          }
        ]
      },
      {
        label: '用户管理',
        path: '/user',
        icon: <UnorderedListOutlined />,
        element: <Outlet />,
        children: [
          {
            path: "/user",
            isHide: true,
            element: <Navigate to="/user/userList" />
          },
          {
            path: '/user/userList',
            label: '用户列表',
            element: lazyLoading('user/userList')
          },
          {
            isHide: true, // 侧边栏隐藏
            path: '/user/show/:id',
            label: '用户列表',
            element: lazyLoading('user/show')
          }
        ]
      },
      {
        label:"商品管理",
        element: <Outlet />,
        icon: <BranchesOutlined />,
        path:'/product',
        children:[
          {
            path:'/product/trademark',
            label:'品牌管理',
            element:lazyLoading('Product/Trademark')
          },
          {
            path:'/product/attr',
            label:'平台属性管理',
            element:lazyLoading('Product/Attr')
          }
        ]
      }
    ]
  }, {
    path: '/login',
    element: lazyLoading('Login')

  }, {
    path: '*',
    element: lazyLoading('NotFound')
  }
]

// - 回用于描述侧边栏的数据
export const renderMenuItem = () => {
  return (routes.find(v => v.path === "/") as any).children;
}



export default function () {
  const router = useRoutes(routes)

  return router
}